using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using UnityEngine.SceneManagement;

public class endGame : MonoBehaviour
{
    public static endGame ins;
    public GameObject g;
    public GameObject star;
    public GameObject displayText;

    [System.NonSerialized] public bool victory = false;
    [System.NonSerialized] public int score = 0;
    [System.NonSerialized] public int perfectPointNumber = 0;
    void Awake()
    {
        ins = this;
        for (int i = 0; i < 3; i++)
        {
            star.transform.GetChild(i).GetComponent<Image>().color = new Vector4(1, 1, 1, 0);
        }
    }
    public void endgame()
    {
        Destroy(SimpleRocket.blast);
        open_close.canvas_open(g);
        if (victory)
        {
            score += 1;

            if (pointManager.number <= level_manager.ins.leve1_perfect_num[level_manager.ins.level])
                score += 1;

            displayText.GetComponent<TextMeshProUGUI>().text = "Victory";
            //
            if (level_manager.ins.level == 4)
            {

                displayText.GetComponent<TextMeshProUGUI>().text = "To Be Contious";
            }
            AudioMgr.Instance.PlayFx(AudioFxType.LevelVictory);
            AudioMgr.Instance.PauseBgm();
            level_manager.ins.passlevel();
        }
        else
        {
            score = 0;
            displayText.GetComponent<TextMeshProUGUI>().text = "Fail";
            //
            AudioMgr.Instance.PlayFx(AudioFxType.LevelFail);
            AudioMgr.Instance.PauseBgm();
        }
        if (score > 3) score = 3;
        for (int i = 0; i < score; i++)
        {
            star.transform.GetChild(i).GetComponent<Image>().color = new Vector4(1, 1, 1, 1);
        }
    }
    public void flush_level()
    {
        AudioMgr.Instance.PlayGameBgm(AudioBgmType.BGM02);
        open_close.canvas_close(g);
        Destroy(SimpleRocket.blast);

        level_manager.ins.flushLevel();
    }
    public void next_level()
    {
        if (victory)
        {
            if (level_manager.ins.level == 4)
            {
                SceneManager.LoadScene(0);
            }
            AudioMgr.Instance.PlayGameBgm(AudioBgmType.BGM02);
            level_manager.ins.nextLevel();
            open_close.canvas_close(g);

        }
        else
        {
            displayText.GetComponent<TextMeshProUGUI>().text = "please pass the level";
        }
    }
    public void plusscore()
    {
        score += 1;
    }
    private void setPerfectPointNumber(int num)
    {
        perfectPointNumber = 0;
    }
    public void init(int num)
    {
        for (int i = 0; i < 3; i++)
        {
            star.transform.GetChild(i).GetComponent<Image>().color = new Vector4(1, 1, 1, 0);
        }
        setPerfectPointNumber(num);
        score = 0;
        victory = false;
    }
}
